package com.testgmaillike;

import com.learnNcode.gmaillikeloading.CustomView;
import com.learnNcode.gmaillikeloading.IActionBarReset;
import com.learnNcode.gmaillikeloading.IRefreshListner;
import com.testgmaillike.callback.RefreshCallback;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.widget.BaseAdapter;

public class BaseActivity extends Activity implements IRefreshListner, IActionBarReset{

	private CustomView mCustomView;
	private ActionBar mActionBar;
	protected RefreshCallback mCallback;
	private Handler handler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	protected void init(Context context) {
		this.initActionBar();
		this.initCustomView(context);
	}
	
	/**
	 * 初始化ActionBar
	 */
	private void initActionBar(){
		mActionBar = getActionBar();
		setActionBarDisplayHomeAsUpEnabled(true);
		setActionBartitle("test");
	}
	
	/**
	 * 初始化customView
	 */
	private void initCustomView(Context context) {
		mCustomView = new CustomView(context, mActionBar);
		setContentView(mCustomView);
		mCustomView.setRefreshListner(this);
		mCustomView.setActionBar(this);
	}
	
	/**
	 * 设置acitionBar的title
	 * @param title
	 */
	private void setActionBartitle(String title){
		mActionBar.setTitle(title);
	}
	
	/**
	 * 设置listview显示
	 * @param adapter
	 */
	protected void setAdapter(BaseAdapter adapter){
		mCustomView.getListView().setAdapter(adapter);
	}
	
	/**
	 * 设置actionbar左边的图是否可以点击
	 * @param flag
	 */
	protected void setActionBarDisplayHomeAsUpEnabled(boolean flag){
		mActionBar.setDisplayHomeAsUpEnabled(flag);
	}
	@Override
	public void onActionBarReset() {
		mActionBar = getActionBar();
		setActionBarDisplayHomeAsUpEnabled(true);
	}
	@Override
	public void preRefresh() {
		mCallback.before();
		handler.postDelayed(runnable, 5000);
	}
	@Override
	public void postRefresh() {
		mCallback.after();
	}
	
	Runnable runnable = new Runnable() {

		@Override
		public void run() {
			mCustomView.getListView().invalidate();
			mCustomView.getListView().requestLayout();
			mCustomView.stopLoading();	
		}
	};
}
